﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Media.Imaging;

namespace RobotEscape.Screens
{
    public class FinalWinScreen : ScreenBase
    {
        GameScreen Parent;
        Rectangle Rectangle = new Rectangle();
        public FinalWinScreen(GameScreen gameScreen)
        {
            Parent = gameScreen;
        }

        public override void Init()
        {
            Canvas.SetLeft(Rectangle, 50);
            Canvas.SetTop(Rectangle, 50);
            Rectangle.Width = Game.Instance.CANVAS_WIDTH - 100;
            Rectangle.Height = Game.Instance.CANVAS_HEIGHT - 100;
            Game.Instance.Canvas.Children.Add(Rectangle);

            base.Init();
        }

        public override void Update(GameTime time)
        {
            ProcessKeys();
            Paint();
            base.Update(time);
        }

        private void ProcessKeys()
        {
            if (KeyState.GetNewlyPressedKey(Key.Space) || KeyState.GetNewlyPressedKey(Key.Enter))
            {
                this.Parent.Remove();
                Game.Instance.SwitchScreen(new GameScreen(1));
            }
        }

        private void Paint()
        {
            WriteableBitmap wb = new WriteableBitmap((int)this.Rectangle.Width, (int)this.Rectangle.Height);
            wb.Invalidate();
            wb.FillRectangle(0, 0, wb.PixelWidth, wb.PixelHeight, Util.BackgroundGrey);

            Util.DrawText(wb, "GAME COMPLETED!", 20, wb.PixelWidth / 2, 0, Colors.White, TextAlignment.Center);

            Util.DrawText(wb, "CONGRADULATIONS, ROBOT.  YOU HAVE EARNED YOUR FREEDOM.", 15, wb.PixelWidth / 2, 50, Colors.White, TextAlignment.Center);

            Util.DrawText(wb, "SPACEBAR TO RESTART", 15, wb.PixelWidth / 2, wb.PixelHeight - 50, Colors.White, TextAlignment.Center);

            WriteableBitmap wbSource = new WriteableBitmap(ResourceHelper.GetBitmap("Assets/images/robotescape_logo.png"));
            wb.Invalidate();
            wb.Blit(new Rect(wb.PixelWidth / 2 - wbSource.PixelWidth / 2, 120, wbSource.PixelWidth, wbSource.PixelHeight), wbSource, new Rect(0, 0, wbSource.PixelWidth, wbSource.PixelHeight));
            wb.Invalidate();

            var imageBrush = new ImageBrush
            {
                Stretch = Stretch.None,
                AlignmentX = AlignmentX.Left,
                AlignmentY = AlignmentY.Top
            };
            imageBrush.ImageSource = wb;
            this.Rectangle.Fill = imageBrush;
        }
        public override void Remove()
        {
            if (Game.Instance.Canvas.Children.Contains(Rectangle))
                Game.Instance.Canvas.Children.Remove(Rectangle);
            base.Remove();
        }
    }
}
